<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;
use Think\Model;
/**
 * Description of AreaController
 *
 * @author Administrator
 */
class AreaController extends BaseController {
    function arealist(){
    	$AREA = new Model('Area');
		$totalRows = $AREA->count();
    	$page = new \Think\Page($totalRows, 10);
    	$arealist = $AREA->limit($page->firstRow,$page->listRows)->order('id desc')->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('arealist',$arealist);
    	$this->display('area-arealist');
    }

    function insert(){
    	if(IS_POST){
            $AREA = D('Area');
            if(!$AREA->create()){
                $this->error($AREA->getError());
            } else {
                $rs = $AREA->add();
                if($rs !== FALSE){
                    $this->success('增加成功');
                } else {
                    $this->error('增加失败');
                }
            }
    	}else{
    		$AREA = new Model('Area');
    		$top = $AREA->where(['pid'=>0])->order('id asc')->select();
    		$this->assign('top',$top);
    		$this->display('area-insert');
    	}
    }

    function delete(){
    	$id = I('id','');
        if($id == ''){
            $this->error('参数错误');
        } else {
            #2，删除操作
            $AREA = new Model('Area');
            $rs = $AREA->where(['id'=>$id])->delete();
            if($rs !== FALSE){
                $this->success('删除成功');
            } else {
                $this->error($AREA->getDbError());
            }
        }
    }

    function edit(){
    	if(IS_POST){
			$id = I('id','');
            if($id != ''){
                $map['id'] = $id;
                if(I('name') != ''){
                    $map['name'] = I('name');
                }
                $map['pid'] = I('pid');
                $AREA = D('Area');
                if(!$AREA->create($map)){
                    $this->error($AREA->getError());
                } else {
                    $rs = $AREA->save();
                    if($rs !== FALSE){
                        $this->success('编辑成功');
                    } else {
                        $this->error('编辑失败');
                    }
                }
            } else {
                $this->error('参数错误');
            }
    	}else{
    		$id = I('id','');
            if($id != ''){
                $AREA = new Model('Area');
                $edit = $AREA->where(['id'=>$id])->find();
                if($edit){
                    $this->assign('edit',$edit);
                    $this->display('area-edit');
                } else {
                    $this->error('区域不存在');
                }
            }else{
                $this->error('参数错误');
            }
    	}
    }

}
